package com.twu.biblioteca.actions;

import com.twu.biblioteca.exceptions.ArgumentError;
import com.twu.biblioteca.exceptions.BaseError;
import com.twu.biblioteca.helper.Scanner;
import com.twu.biblioteca.models.Book;
import com.twu.biblioteca.persistences.Books;


public class BorrowBookAction extends BaseAction {
    private Books allBooks;

    public BorrowBookAction(Books rootBooksInstance){
        allBooks = rootBooksInstance;
    }

    @Override
    public void run() throws BaseError {
        super.run();

        Books availableBooks = allBooks.available();
        availableBooks.print();
        printPrompts();

        Integer bookID = readInteger();
        Book borrowedBook = availableBooks.find(bookID);
        borrowedBook.borrow();

        printSuccessMessage();
    }

    @Override
    public String actionName() {
        return "Borrow a Book";
    }

    protected void printSuccessMessage() {
        System.out.println("Thank you! Enjoy the book");
    }

    protected void printPrompts(){
        System.out.print("Write the ID of the book you want to borrow: ");
    }

    protected Integer readInteger() throws ArgumentError {
        Scanner scanner = new Scanner();
        return scanner.readInteger();
    }
}
